package com.flyoil.petromp.ui.activity.activity_matter;

import android.content.Intent;
import android.view.View;
import android.widget.TextView;

import com.flyoil.petromp.Constant.ConstantNetworkCode;
import com.flyoil.petromp.mvp_p.Listener.BasePresenter;
import com.flyoil.petromp.R;
import com.flyoil.petromp.adapter.adapter_matter.ItemMatterAdapter;
import com.flyoil.petromp.base.BaseActivity;
import com.flyoil.petromp.base.BaseRecyAdapter;
import com.flyoil.petromp.mvp_p.ListPresenter.MatterListPresenter;
import com.flyoil.petromp.mvp_v.ListUIView.BaseUIView;
import com.flyoil.petromp.entity.entity_matter.MatterEntity;
import com.flyoil.petromp.ui.activity.activity_repertory.ProjectMessageActivity;
import com.flyoil.petromp.ui.activity.activity_repertory.ServiceMessageActivity;
import com.flyoil.petromp.ui.activity.activity_repertory.SupplierMessageActivity;
import com.flyoil.petromp.utils.HintConstants;
import com.flyoil.petromp.utils.OrderUtl.OrderListUtil;
import com.flyoil.petromp.view.SearchDataHint;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 代办事项、已办事项的列表界面
 */
public class MatterListActivity extends BaseActivity implements BaseUIView {
    private ItemMatterAdapter adapter;
    private int matter_type = 0;//事项的类型 1：待办事项 2：已办事项
    private BasePresenter presenter;
    private TextView tv_condition;
    private String billType = "";

    @Override
    protected int getContentViewResId() {
        return R.layout.activity_matter_list;
    }

    @Override
    protected void initViews() {
        super.initViews();
        matter_type = getIntent().getIntExtra("matter_type", 0);
        if (matter_type == 1) {
            setTextTitleName("待办事项");
        } else {
            setTextTitleName("已办事项");
        }
        recyclerView = $(R.id.list_matter_list);
        setRecyviewLayoutManager(null);
        adapter = new ItemMatterAdapter(mContext);
        recyclerView.setAdapter(adapter);
        setBackOnclickListner(mContext);
        setTextRightName("筛选");
        tv_condition = $(R.id.tv_matter_list_condition_name);
        presenter = new MatterListPresenter(this, matter_type + "");
    }

    @Override
    protected void initEvents() {
        super.initEvents();
        adapter.setOnItemClickListener(onItemClickListener);
        setOnRefreshListener(true);
        setTextRightOnclickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(mContext, SearchOrderConditionActivity.class);
                startActivityForResult(intent, 100);
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        //选择的帅选条件，界面重新加载数据
        if (requestCode == 100 && resultCode == RESULT_OK) {
            if (data != null) {
                if (data.getStringExtra("billType") != null) {
                    billType = data.getStringExtra("billType");
                    isFirstLoadck = true;
                    onRefreshData();
                    if (data.getStringExtra("name") != null && data.getStringExtra("billType").length() > 0) {
                        tv_condition.setText(data.getStringExtra("name"));
                        tv_condition.setVisibility(View.VISIBLE);
                    } else {
                        tv_condition.setVisibility(View.GONE);
                    }
                }

            } else {
                tv_condition.setVisibility(View.GONE);
            }
        }
        //对单据进行了操作，返回列表是刷新数据刷新数据
        if (requestCode == 1010 && resultCode == RESULT_OK) {
            onRefreshData();
        }
    }

    /**
     * 列表item的点击事件
     */
    private BaseRecyAdapter.OnItemClickListener onItemClickListener = new BaseRecyAdapter.OnItemClickListener() {
        @Override
        public void Onclick(int position, Object data) {
            Intent intent = new Intent();
            MatterEntity entity = (MatterEntity) data;
            switch (entity.getBillType()) {
                case OrderListUtil.REPERTORY_SUPPLIER:
                    intent.setClass(mContext, SupplierMessageActivity.class);
                    break;
                case OrderListUtil.REPERTORY_SERVICE:
                    intent.setClass(mContext, ServiceMessageActivity.class);
                    break;
                case OrderListUtil.REPERTORY_PRODUCT:
                    intent.setClass(mContext, ProjectMessageActivity.class);
                    break;
                default:
                    intent.setClass(mContext, OrderMessageActivity.class);
                    break;
            }
            intent.putExtra("order_type", entity.getBillType());
            intent.putExtra("id", entity.getId());
            startActivityForResult(intent, 1010);
        }
    };


    @Override
    protected void initdatas() {
        super.initdatas();
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("billType", billType);
        map.put("pageNumber", pageNumber);
        map.put("pageSize", pageSize);
        presenter.initNetworkData(map);
    }

    @Override
    public void updataToUI(List data) {
        if (data != null) {
            if (isFirstLoadck) {
                isFirstLoadck = false;
                adapter.setDataList(data);
                recyclerView.notifyDataSetChanged();
                if (data.size() > 0) {
                    onDataSucceed();
                    pageNumber++;
                } else {
                    onDataNull(null);
                }
            } else {
                if (pageNumber == 1) {
                    adapter.setDataList(data);
                    recyclerView.notifyDataSetChanged();
                    onRefreshDataSucceed();
                } else {
                    adapter.addBottonDatas(data);
                    recyclerView.notifyDataSetChanged();
                    if (data.size() < pageSize) {
                        onLoadDataFinish();
                    } else {
                        onLoadDataSucceed();
                    }
                }
                pageNumber++;
            }

        } else {
            if (isFirstLoadck) {
                onDataNull(null);
            } else {
                if (pageNumber == 1) {
                    onRefreshDataError();
                } else {
                    onLoadDataError();
                }
            }

        }


    }

    @Override
    protected void onRefreshData() {
        super.onRefreshData();
        initdatas();
    }

    @Override
    protected void onLoadData() {
        super.onLoadData();
        initdatas();
    }
}
